<?php
    class model_cart extends database_mysql
    {

        public function mdc_cart_list($cid = null , $page = 1 , $limit = 50)
        {
            $tb_cart = prefix . "cart";
            $tb_cart_detail = prefix . "cart_detail";
            $str = null;
            if (!empty($limit)) {
                $page = ($page == 0 || empty($page) ? 1 : $page);
                $start = ($page - 1) * $limit;
                $strlimit = "LIMIT $start, $limit";
            } else {
                $strlimit = '';
            }

            if ($cid) {
                $cid = parent::mysql_check_string($cid);
                $str = "WHERE $tb_cart.CID = '$cid' AND ";
            }
            $sql = "
                SELECT DISTINCT  $tb_cart.*
                FROM
                $tb_cart RIGHT JOIN $tb_cart_detail
                ON $tb_cart.ID = $tb_cart_detail.CartID
                " . $str . "
                ORDER BY $tb_cart.PostDate DESC " . $strlimit . "
        ";
            $row = parent::mysql_fetch_all_rows($sql);
            parent::mysql_close_connection();
            return $row;
        }

        public function mdc_cart_total_list($cid = null)
        {
            $tb_cart = prefix . "cart";
            $tb_cart_detail = prefix . "cart_detail";
            $str = null;
            if ($cid) {
                $cid = parent::mysql_check_string($cid);
                $str = "WHERE $tb_cart.CID = '" . $cid . "'  AND ";
            }
           $sql = "
                SELECT
                count(DISTINCT `ID`) as tsd
                FROM
                $tb_cart INNER JOIN $tb_cart_detail
                ON $tb_cart.ID = $tb_cart_detail.CartID
                " . $str . "
                ORDER BY $tb_cart.ID DESC
        ";
            $row = parent::mysql_fetch_all_rows($sql);
            $tsd = isset($row[0]['tsd']) ? $row[0]['tsd'] : 0;

            parent::mysql_close_connection();

            return $tsd;
        }

        public function mdp_product_get_cid($seokey = null)
        {
            $str = null;
            $row = parent::mysql_fetch_one_rows("
                SELECT
                *
                FROM
                " . prefix . "product INNER JOIN " . prefix . "product_lang
                ON " . prefix . "product.ID = " . prefix . "product_lang.ProductID
                WHERE
                " . prefix . "product_lang.SeoKey = '" . $seokey . "'
                " . prefix . "product_lang.LangID = '" . LANG . "'
        ");

            parent::mysql_close_connection();

            return $row;
        }

        public function mdc_get_cat_cid($seokey)
        {
            $row = null;
            if (!empty($seokey)) {
                $seokey = parent::mysql_check_string($seokey);
                $row = parent::mysql_fetch_all_rows("SELECT  * FROM " . prefix . "categories_lang WHERE SeoKey = '" . trim($seokey) . "' AND LangID = '" . LANG . "'");
                parent::mysql_close_connection();
            }
            return $row;
        }

        public function mdc_get_cat_pid($ID)
        {
            $row = null;
            if (!empty($ID)) {
                $ID = parent::mysql_check_string($ID);
                $sql="SELECT
                    prefix_categories.*,
                    prefix_categories_lang.CatName
                    FROM
                    prefix_categories_lang
                    INNER JOIN prefix_categories ON prefix_categories_lang.CatID = prefix_categories.ID
                    WHERE
                    prefix_categories.PID ='$ID'";
                $row = parent::mysql_fetch_all_rows($sql);
                parent::mysql_close_connection();
            }
            return $row;
        }


        public function mdp_get_art($artid)
        {
            $artid = parent::mysql_check_string($artid);

            $row = parent::mysql_fetch_one_rows("
            SELECT * FROM
                " . prefix . "product
            WHERE
                " . prefix . "product.ID = '" . trim($artid) . "'
        ");

            parent::mysql_close_connection();

            return $row;
        }

        public function mdp_tool_func($model , $active , $str_id)
        {
            $done = false;
            $tb_cart = prefix . "cart";
            $tb_cart_detail = prefix . "cart_detail";
            switch ($active) {
                case 'notview':
                    $done = $model->model_tool_set_status($tb_cart , 'notview' , " `ID` IN(" . trim($str_id) . ")");
                    break;
                case 'process':
                    $done = $model->model_tool_set_status($tb_cart , 'process' , "`ID` IN(" . trim($str_id) . ")");
                    break;
                case 'finished':
                    $done = $model->model_tool_set_status($tb_cart , 'finished' , " `ID` IN(" . trim($str_id) . ")");
                    break;
                case 'cancel':
                    $done = $model->model_tool_set_status($tb_cart , 'cancel' , " `ID` IN(" . trim($str_id) . ")");
                    break;
                case 'checkout':
                    $done = $model->model_tool_set_status($tb_cart , 'checkout' , " `ID` IN(" . trim($str_id) . ")");
                    break;

                case 'delete':
                    $done = $model->model_tool_delete($tb_cart , " `ID` IN(" . trim($str_id) . ")");
                    $done=$model->model_tool_delete($tb_cart_detail , " CartID IN(" . trim($str_id) . ")");

                    break;
            }

            return $done;
        }

        public  function  mdc_GetCartDetail($ID)
        {
            $tb_cart = prefix . "cart";
            $tb_cart_detail = prefix . "cart_detail";
            $tb_checkout = prefix."check_out";
            $tb_product_lang = prefix."product_lang";
            $sql = "SELECT
                            $tb_cart_detail.CartID,
                            $tb_cart_detail.ProductID,
                            prefix_cart_detail.Amount,
                            $tb_cart_detail.Price,
                            prefix_product_lang.`Name`,
                            $tb_cart.PostDate,
                            $tb_cart.Status,
                            $tb_checkout.`Name` as thanhtoan
                            FROM
                            $tb_cart_detail
                            INNER JOIN $tb_cart ON $tb_cart.ID = $tb_cart_detail.CartID
                            INNER JOIN prefix_product_lang ON $tb_cart_detail.ProductID = $tb_product_lang.productID
                            INNER JOIN $tb_checkout ON $tb_cart.CheckoutID = $tb_checkout.ID
                            WHERE $tb_cart.ID = '$ID'";
            $row = parent::mysql_fetch_all_rows($sql);
            parent::mysql_close_connection();
            return isset($row) ? $row : null;
        }

        public  function  mdc_GetSentMail($ID)
        {
            $tb_cart = prefix . "cart";
            $sql ="SELECT `SentMail`,`ID` FROM `$tb_cart` WHERE  `ID` in ($ID)";
           $row= parent::mysql_fetch_all_rows($sql);
            return $row;
        }

        public  function  mdc_delete($ID)
        {
            $tb_cart = prefix . "cart";
            $sql ="DELETE FROM `$tb_cart` WHERE `ID`='$ID'";
            parent::mysql_update_insert($sql);
        }

        public  function  mdc_UpdateSentMail($status,$ID)
        {
            $tb_cart = prefix . "cart";
            $sql ="UPDATE `$tb_cart` SET `SentMail`='$status' WHERE `ID` ='$ID'";
             parent::mysql_update_insert($sql);
        }

        public  function mdc_GetBuyerByID($ID)
        {
            $tb_cart = prefix . "cart";
            $tb_checkout = prefix."check_out";
            $sql ="SELECT DISTINCT
                        $tb_cart.*,
                        $tb_checkout.`Name` as thanhtoan
                        FROM
                        $tb_cart
                        INNER JOIN $tb_checkout ON $tb_cart.CheckoutID = $tb_checkout.ID
                        WHERE
                        $tb_cart.ID = '$ID'";
            $row = parent::mysql_fetch_all_rows($sql);
            parent::mysql_close_connection();
            return isset($row) ? $row : null;
        }

    }